#include "mpp_display_element.h"

mpp_display_element::mpp_display_element(string dev_path):
                                                        mpp_display_dev(dev_path)
{
   
}

mpp_display_element::~mpp_display_element()
{
    
}


vector<drmModeConnectorPtr>  mpp_display_element::mpp_element_create_connect(int fd,uint32_t count_connectors,uint32_t *connectors)
{
      uint32_t i = 0;
      vector<drmModeConnectorPtr> conn;
      conn.clear();
      printf("开始查找连接器\n");
      for (i = 0; i <count_connectors; i++) {     // --拿到所有连接器
         drmModeConnectorPtr  tmp= drmModeGetConnector(fd,connectors[i]);   
         if (!tmp) {
            printf("failed to get connector %d\n", i);
            break;
         }
         printf("找到连接器 ID:%d\n",tmp->connector_id);
         conn.push_back(tmp);
      }
      return conn;
}
void mpp_display_element::mpp_element_distroy_connect(int fd, vector<drmModeConnectorPtr> connector)
{
      if(!connector.empty()){
         for(int i=0;i<connector.size();i++){
            if(connector.at(i))
               drmModeFreeConnector(connector.at(i));
         }
      }
      connector.clear();
}

vector<drmModeEncoderPtr>  mpp_display_element::mpp_element_create_encoder(int fd,uint32_t count_encoders,uint32_t *encoders)
 {
      uint32_t i = 0;
      vector<drmModeEncoderPtr> encoder;
      encoder.clear();
      printf("开始查找编码器\n");
      for (i = 0; i <count_encoders; i++) {     // --拿到所有编码器
      drmModeEncoderPtr  tmp= drmModeGetEncoder(fd,encoders[i]);   
      if (!tmp) {
         printf("failed to get encoder %d\n", i);
         break;
      }
      printf("找到编码器 ID:%d\n",tmp->encoder_id);
      encoder.push_back(tmp);
      }
      return encoder;
 }
 void mpp_display_element::mpp_element_distroy_encoder(int fd,vector<drmModeEncoderPtr> encoders)
 {
      if(!encoders.empty()){
         for(int i=0;i<encoders.size();i++){
            if(encoders.at(i))
               drmModeFreeEncoder(encoders.at(i));
         }
      }
      encoders.clear();
 }


 
vector<drmModeCrtcPtr> mpp_display_element::mpp_element_create_crtc(int fd,uint32_t count_crtcs,uint32_t *crtcs)
 {
     uint32_t i = 0;
      vector<drmModeCrtcPtr> crtc;
      crtc.clear();
       printf("开始查找扫描器\n");
      for (i = 0; i <count_crtcs; i++) {     // --拿到所有扫描器
         drmModeCrtcPtr  tmp= drmModeGetCrtc(fd,crtcs[i]);   
         if (!tmp) {
            printf("failed to get encoder %d\n", i);
            break;
         }
         printf("找到扫描器 ID:%d\n",tmp->crtc_id);
         crtc.push_back(tmp);
      }
      if(mpp_planes_create(fd,count_crtcs)){//--获取图层
             crtc.clear();
             return crtc;
      }
      return crtc;

 }
 void mpp_display_element::mpp_element_distroy_crtc(int fd,vector<drmModeCrtcPtr>crtc)
 {
    if(!crtc.empty()){
         for(int i=0;i<crtc.size();i++){
            if(crtc.at(i))
               drmModeFreeCrtc(crtc.at(i));
         }
      }
      crtc.clear();
      mpp_planes_distroy();

   // uint32_t i = 0;
   // if (this->crtcs) {
   //    for (i = 0; i < this->num_crtcs; i++) {
   //       if (this->crtcs[i])
   //             drmModeFreeCrtc(this->crtcs[i]);
   //    }
   //    free(this->crtcs);
   //    this->crtcs = NULL;
   // }
   //  mpp_planes_distroy();
 }